package com.front.web.member;

import com.bootdo.common.redisUtils.JedisUtils;
import com.bootdo.common.utils.R;
import com.bootdo.mall.domain.GoodSpecs;
import com.bootdo.mall.domain.MallGood;
import com.bootdo.mall.service.GoodSpecsService;
import com.bootdo.member.domain.ShequMember;
import com.front.web.commons.BrowseHistory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 浏览记录前端接口
 */
@RestController
@RequestMapping("/f/shequ/BrowseHistoryController/")
public class BrowseHistoryController extends MemberSuperController{

    public final String BH="BrowseHistory_HuanLeGou";

    /**
     * 获取浏览记录
     * @param mobile
     * @return
     */
    @RequestMapping("getBrowseHistory")
    public R getBrowseHistory(@RequestParam(value = "mobile",defaultValue = "") String mobile){



        if(JedisUtils.exists(BH)){
            Map<String, Object> mapData = JedisUtils.getObjectMap(BH);
            List<BrowseHistory> list = (List<BrowseHistory>)mapData.get(mobile);

            return R.ok().put("i",list);
        }else{
            return R.ok();
        }

    }


    /**
     * 删除浏览记录
     * @param mobile
     * @param goodId
     * @return
     */
    @RequestMapping("delBrowseHistory")
    public R delBrowseHistory(@RequestParam(value = "mobile",defaultValue = "") String mobile,
                              @RequestParam(value = "goodId",defaultValue = "") Long goodId){

        Map<String, Object> mapData = JedisUtils.getObjectMap(BH);

        List<BrowseHistory> list = (List<BrowseHistory>)mapData.get(mobile);

        Iterator<BrowseHistory> it = list.iterator();
        while (it.hasNext()){
            if(it.next().getId().longValue()==goodId.longValue()){
                it.remove();
            }
        }
        return R.ok().put("i",list);
    }

    /**
     * 放入浏览记录
     * @param mobile
     * @param goodId
     * @return
     */
    @RequestMapping("setBrowseHistory")
    public R setBrowseHistory(@RequestParam(value = "mobile",defaultValue = "") String mobile,
                              @RequestParam(value = "goodId",defaultValue = "") Long goodId){
        MallGood mallGood = mallGoodService.get(goodId);
        ShequMember member = shequMemberService.getByMobile(mobile);
        if(mallGood==null||member==null){
            return R.error("非法请求");
        }
        Thread thread = new Thread(new Runnable() {
            @Override
            public synchronized void run() {

                BrowseHistory browseHistory = new BrowseHistory();//准备一个  商品临时实体对象
                ArrayList<BrowseHistory> list = new ArrayList<>();//准备一个 ArrayList 集合 放入 临时实体对象
                //往临时实体类中添加数据
                browseHistory.setId(mallGood.getId());
                browseHistory.setName(mallGood.getName());
                browseHistory.setImgUrl(mallGood.getPic());
                browseHistory.setCreateTime(new Date());
                if(JedisUtils.exists(BH)){
                    Map<String, Object> param = JedisUtils.getObjectMap(BH);
                    if(param.containsKey(member.getMobile())){
                        List<BrowseHistory> historyList=(List<BrowseHistory>) param.get(member.getMobile());
                        //删除该key值记录，重新放入第一位
                        Iterator<BrowseHistory> it = historyList.iterator();
                        while (it.hasNext()){
                            if(it.next().getId().longValue()==goodId.longValue()){
                                it.remove();
                            }
                        }
                        if(historyList.size()>=100){
                            historyList.remove(historyList.size()-1);
                        }
                        historyList.add(0,browseHistory);

                        param.remove(mobile);
                        param.put(mobile,historyList);
                        JedisUtils.setObjectMap(BH,param,0);
                    }else{
                        list.add(browseHistory);
                        param.put(member.getMobile(),list);
                        JedisUtils.setObjectMap(BH,param,0);
                    }
                }else{
                    HashMap<String, Object> map = new HashMap<>();//创建一个map对象准备初始化数据

                    list.add(browseHistory);

                    map.put(member.getMobile(),list);

                    JedisUtils.setObjectMap(BH,map,0);
                }
            }
        });
        thread.start();

        return R.ok();
    }

}
